//
//  YZMRankCell.m
//  YZMicroStock
//
//  Created by apple on 17/3/7.
//  Copyright © 2017年 cqjr. All rights reserved.
//

#import "YZMRankCell.h"
#import "NSNumber+Utils.h"

@interface YZMRankCell ()
@property (nonatomic, strong) UIImageView *rankImageView;
@property (nonatomic, strong) UILabel *rankLabel;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *numberLabel;
@property (nonatomic, strong) UILabel *detailLabel;
@property(nonatomic, strong) UIButton *btnTrade;

@end

@implementation YZMRankCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createObjects];
        self.rightMargin = 10.0;
        self.leftMargin = 10.0;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return self;
}

- (void)createObjects{
    
//    self.rankImageView = [UIImageView new];
//    [self.contentView addSubview:self.rankImageView];
//    [self.rankImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.contentView).offset(15);
//        make.top.equalTo(self.contentView).offset(12);
//    }];
//
//    self.rankLabel = [YZMAppStyle labelWithSmallWhite];
//    [self.contentView addSubview:self.rankLabel];
//    [self.rankLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.contentView).offset(20);
//        make.left.equalTo(self.contentView).offset(25);
//    }];
    
    self.nameLabel = [YZMAppStyle labelWithMediumWhite];
    [self.contentView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(20);
        make.top.equalTo(self.contentView).offset(14);
//        make.width.mas_equalTo(70);
    }];
    
    self.numberLabel = [YZMAppStyle labelWithSmallLightGray];
    [self.contentView addSubview:self.numberLabel];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.nameLabel.mas_bottom).offset(6);
        make.width.mas_equalTo(ScreenWidth*0.3);
        make.left.equalTo(self.nameLabel);
//        make.centerX.equalTo(self.contentView);
    }];
    self.numberLabel.textAlignment = NSTextAlignmentLeft;
    
    self.detailLabel = [YZMAppStyle labelWithSmallLightGray];
    self.detailLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:self.detailLabel];
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.numberLabel);
//        make.width.mas_equalTo(ScreenWidth*0.3);
        make.left.equalTo(self.numberLabel.mas_right).offset(10);
    }];
    
    UIButton *btnTap = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:btnTap];
    [btnTap mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self.contentView).offset(-10);
        make.width.equalTo(@60);
        make.height.equalTo(@30);
    }];
    [btnTap addTarget:self action:@selector(trade:) forControlEvents:UIControlEventTouchUpInside];
    
    self.btnTrade = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnTrade.userInteractionEnabled = NO;
    self.btnTrade.titleLabel.font = [YZMAppStyle smallSizeFont];
    [self.btnTrade setTitle:@"点买" forState:UIControlStateNormal];
    [self.btnTrade setTitleColor:[YZMAppStyle colorForYellowButton] forState:UIControlStateNormal];
    [self.contentView addSubview:self.btnTrade];
    [self.btnTrade mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.width.equalTo(@40);
        if (ScreenWidth == 320) {
            make.right.equalTo(self.contentView).offset(-10);
            make.height.equalTo(@20);
        }else{
            make.right.equalTo(self.contentView).offset(-20);
            make.height.equalTo(@25);
        }
        
    }];
    [YZMSetBorderAndCornerRadius setView:self.btnTrade borderWidth:1.0 borderColor:[YZMAppStyle colorForYellowButton] cornerRadius:4.0];
    
    if (iPhone4 || iPhone5) {
        self.nameLabel.font = [YZMAppStyle fontWithSize:11];
        self.numberLabel.font = [YZMAppStyle fontWithSize:11];
        self.detailLabel.font = [YZMAppStyle fontWithSize:11];
    }
}

-(void)setIndexForImgageView:(NSInteger )indexForImgageView{
    
    _indexForImgageView = indexForImgageView;
    if (indexForImgageView<=2) {
        
        [self.rankImageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Image_rank_cell%@",@(indexForImgageView+1)]]];
        self.rankLabel.hidden = YES;
        
    }else{
        
        self.rankImageView.hidden = YES;
        self.rankLabel.text = [NSString stringWithFormat:@"%@",@(indexForImgageView+1)];
    }
    
}

-(void)setRankModel:(YZMHomeRankListModel *)rankModel{
    
    _rankModel = rankModel;
    _nameLabel.text = rankModel.userName;
    _numberLabel.text = [NSString stringWithFormat:@"点卖了%@条策略",@(rankModel.strategyCount)];
    NSString* detailPrice = [NSString stringWithFormat:@"%@",@(rankModel.profit).r4fSeparator];
    NSString *detailString = [NSString stringWithFormat:@"盈利%@元",detailPrice];
    NSMutableAttributedString *detailAttString = [[NSMutableAttributedString alloc]initWithString:detailString];
    [detailAttString addAttribute:NSForegroundColorAttributeName value:[YZMAppStyle colorForIconColor] range:NSMakeRange(detailString.length-detailPrice.length-1,detailPrice.length+1)];
    
    self.detailLabel.attributedText = detailAttString;
}

- (void)trade:(UIButton *)btn{
    if (self.handle) {
        self.handle();
    }
}

@end
